//测试基地址
/* document.querySelector('#btn-register').addEventListener('click', e => {
    axios({
        url: '/register',
        data: {
            username: 'laoqu666',
            password: '123456'
        },
        method: 'post'
    })
}) */

//用户注册
document.querySelector('#btn-register').addEventListener('click', async e => {
    //获取表单
    const form = document.querySelector('.register-form')
    const data = serialize(form, { hash: true, empty: true })
    const { username, password } = data
    if (username === '' || password === '') {
        showToast('用户名和密码不能为空!')
        return
    }

    if (username.length < 8 || username.length > 30 || password.length < 6 || password.length > 30) {
        showToast('用户名长度要求8-30个字符 密码长度要求6-30个字符')
        return
    }

    //发送请求  
    try {
        const res = await axios.post('/register', data)
        showToast(res.message)
        //跳转登录页  默认填充用户名
        const username = res.data.account
        localStorage.setItem('loginUsername', username)
        setTimeout(e => {
            location.href = './login.html'
        }, 1500)
    } catch (error) {
        showToast(error.response.data.message)

    }


})